package com.spark.lola.service.ser.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spark.lola.entity.sup.SupplierInfo;
import com.spark.lola.mapper.sup.SupplierInfoMapper;
import com.spark.lola.service.ser.SupervisorService;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.common.entity.ShiroUser;
import com.spark.lola.support.wms.cache.CollectionCache;
import com.spark.lola.support.wms.configuration.WmsProperties;
import com.spark.lola.vo.SupervisorCountVo;
import com.spark.lola.vo.SupervisorManageVo;
import com.spark.lola.vo.SupervisorTransactionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * @author: Zhi-peng wang
 * @date: 2020/07/03 13:35
 **/
@Service
public class SupervisorServiceImpl implements SupervisorService {

    @Autowired
    private WmsProperties wmsProperties;

    @Autowired
    private SupplierInfoMapper supplierInfoMapper;

    @Override
    public SupervisorCountVo countNumber(ShiroUser shiroUser) {
        SupervisorCountVo supervisorCountVo = new SupervisorCountVo();
        // 客服主管
        if (shiroUser.getDutyId().equals(wmsProperties.getService().getDutyId().get(0))) {
            // 业务员数量
            supervisorCountVo.setPurNumber(CollectionCache.dutyEmployees.get(wmsProperties.getPurchase().getDutyId().get(1)).size());
            // 客服数量
            supervisorCountVo.setSupervisorNumber(CollectionCache.dutyEmployees.get(wmsProperties.getService().getDutyId().get(1)).size());
            // 供应商数量
            supervisorCountVo.setSupNumber(supplierInfoMapper.countSupervisor("all", null));
            // 待绑定供应商数量
            supervisorCountVo.setNotBindSupNumber(supplierInfoMapper.countSupervisor("notBind", null));
            // 待分配供应商数量
            supervisorCountVo.setNotAllotSupNumber(supplierInfoMapper.countSupervisor("notAllot", null));
        } else {
            // 获取监管的供应商
            QueryWrapper<SupplierInfo> wrapper = new QueryWrapper<>();
            wrapper.select("id,salesman_id");
            wrapper.eq("used", CommonConstant.Y);
            wrapper.eq("service_id", shiroUser.getId());
            List<SupplierInfo> list = supplierInfoMapper.selectList(wrapper);
            int count = list.stream().filter(item -> item.getSalesmanId() != null).collect(Collectors.toList())
                    .stream().collect(Collectors.toCollection(() ->
                            new TreeSet<>(Comparator.comparing(SupplierInfo::getSalesmanId)))).size();
            // 业务员数量
            supervisorCountVo.setPurNumber(count);
            // 我监管的供应商数量
            supervisorCountVo.setAllotSupNumber(list.size());
//            // 我绑定的供应商数量
//            supervisorCountVo.setBindSupNumber(supplierInfoMapper.countSupervisor("bind", shiroUser.getId()));
        }
        return supervisorCountVo;
    }

    @Override
    public SupervisorTransactionVo transactionNumber(ShiroUser shiroUser) {
        SupervisorTransactionVo transactionVo = new SupervisorTransactionVo();
        // 获取所有已绑定供应商
//        QueryWrapper<SupplierInfo> wrapper = new QueryWrapper<>();
//        wrapper.select("service_id,update_time");
//        wrapper.eq("used", CommonConstant.Y);
        List<SupplierInfo> supplierInfoList = supplierInfoMapper.findUsed();
        // 客服主管
        if (shiroUser.getDutyId().equals(wmsProperties.getService().getDutyId().get(0))) {
            // 待绑定供应商数量
            transactionVo.setNotBindSupNumber(supplierInfoMapper.countSupervisor("notBind", null));
            // 待分配供应商数量
            transactionVo.setNotAllotSupNumber(supplierInfoMapper.countSupervisor("notAllot", null));
            // 15-24
            transactionVo.setOneLevelRemindNumber((int) supplierInfoList.stream().filter(item ->
                    item != null &&
                            item.getSalesmanId() != null &&
                            item.getSilence() >= wmsProperties.getService().getExpires().get("oneLevel") &&
                            item.getSilence() < wmsProperties.getService().getExpires().get("twoLevel")).count());
            // >=25
            transactionVo.setTwoLevelRemindNumber((int) supplierInfoList.stream().filter(item ->
                    item != null &&
                            item.getSalesmanId() != null &&
                            item.getSilence() >= wmsProperties.getService().getExpires().get("twoLevel") &&
                            item.getSilence() <= wmsProperties.getService().getExpires().get("reset")).count());
        } else {
            // 15-24
            transactionVo.setOneLevelRemindNumber((int) supplierInfoList.stream().filter(item ->
                    item != null &&
                            item.getSalesmanId() != null &&
                            shiroUser.getId().equals(item.getServiceId()) &&
                            item.getSilence() >= wmsProperties.getService().getExpires().get("oneLevel") &&
                            item.getSilence() < wmsProperties.getService().getExpires().get("twoLevel")).count());
            // >=25
            transactionVo.setTwoLevelRemindNumber((int) supplierInfoList.stream().filter(item ->
                    item != null &&
                            item.getSalesmanId() != null &&
                            shiroUser.getId().equals(item.getServiceId()) &&
                            item.getServiceId().equals(shiroUser.getId()) &&
                            item.getSilence() >= wmsProperties.getService().getExpires().get("twoLevel") &&
                            item.getSilence() <= wmsProperties.getService().getExpires().get("reset")).count());
        }
        return transactionVo;
    }

    @Override
    public List<SupervisorManageVo> finList() {
        List<ShiroUser> shiroUserList = CollectionCache.dutyEmployees.get(wmsProperties.getService().getDutyId().get(1));
        List<SupervisorManageVo> supervisorManageVoList = new ArrayList<>();
        shiroUserList.forEach(item -> {
            QueryWrapper<SupplierInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("name,linkman,mobile,update_time");
            queryWrapper.eq("used", CommonConstant.Y);
            queryWrapper.eq("service_id", item.getId());
            List<SupplierInfo> supplierInfoList = supplierInfoMapper.selectList(queryWrapper);
            SupervisorManageVo supervisorManageVo = new SupervisorManageVo();
            supervisorManageVo.setShiroUser(item);
            supervisorManageVo.setBindSup(supplierInfoList);
            supervisorManageVoList.add(supervisorManageVo);
        });
        return supervisorManageVoList;
    }


}
